package com.cd.university.config.security.validate;

import com.cd.university.config.security.properties.SecurityConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 验证码生成接口
 * @author 陈栋
 * @create 2021/9/4 10:53
 */
@RestController
@Api("验证码的生成")
public class ValidateCodeController {

    @Autowired
    private ValidateCodeProcessorHolder validateCodeProcessorHolder;

    /**
     * 创建验证码，根据验证码类型不同，调用不同的 {@link ValidateCodeProcessor}接口实现
     * @param request 响应和请求
     * @param type 验证码类型
     */
    @GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{type}")
    @ApiOperation("验证码生成接口")
    public void createCode(ServletWebRequest request,
                           @PathVariable("type") String type)
            throws Exception {
        ValidateCodeProcessor validateCodeProcessor = validateCodeProcessorHolder.findValidateCodeProcessor(type);
        // 创建验证码
        validateCodeProcessor.create(request);
    }
}
